package com.huazi.www.doctor.doctorc.servlet;

import com.alibaba.fastjson.JSON;

import com.huazi.www.doctor.doctorc.entity.DrugEntity;
import com.huazi.www.doctor.doctorc.entity.MedicineEntity;
import com.huazi.www.doctor.doctorc.service.impl.MedicineServiceimpl;
import com.huazi.www.doctor.doctorc.util.Page;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class MedicineServlet extends HttpServlet {
    /**
     * 接收MedicineSelect.jsp界面的请求进行缴费处理
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    /**
     * 接收MedicineSelect.jsp界面的请求进行缴费处理
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");//req转utf-8
        resp.setContentType("text/html;charset=UTF-8");//resp转utf-8
        MedicineServiceimpl service = new MedicineServiceimpl();//new  MedicineServiceimpl业务类
        String currPageNo = req.getParameter("currPageNo");//获取卡号id
        String userName = req.getParameter("userName");//获取卡号姓名
        String czf = req.getParameter("czf");//操作符
        String xiugai = req.getParameter("xiugai");//修改
        if (czf == null) {
            czf = "";
        }
        if (currPageNo == null) {
            currPageNo = "1";
        }
        if (userName == null) {
            userName = "";
        }
        //得到药房所有药品的集合
        List<DrugEntity> list = service.list();//list<DrugEntity>的泛型集合
        req.setAttribute("list", list);//req保存list

        //删除操作
        if (czf.equals("delete")) {
            //转换成数字
            int id = Integer.parseInt(req.getParameter("id"));//获取id转为int类型
            service.delete(id);//给删除方法传入id
            PrintWriter pw = resp.getWriter();//输出流
            String boo = JSON.toJSONString("true");//转JSON格式
            pw.print(boo);//写出要输出的东西
            pw.flush();//刷新
            pw.close();//关闭
        } else if (xiugai != null) {//修改密码
            String doctorid_id = req.getParameter("doctorid_id");//医生id编号
            String doctorPwdyuan_pwd = req.getParameter("doctorPwdyuan_pwd");//医生旧密码
            String doctorPwdxin_pwd = req.getParameter("doctorPwdxin_pwd");//医生新密码
            String doctorPwdOk_pwd = req.getParameter("doctorPwdOk_pwd");//医生确认密码
            String doctorid = req.getParameter("doctorid");//编号
            boolean fale = service.pwdSelectOK(doctorid_id, doctorPwdyuan_pwd, doctorPwdxin_pwd, doctorPwdOk_pwd);//给pwdSelectOK传入医生id编号，医生旧密码，医生新密码，医生确认密码
            req.setAttribute("doctorid", doctorid);//保存编号
            req.getRequestDispatcher("Userr/ModifyPwd.jsp").forward(req, resp);//转发ModifyPwd.jsp页面
        } else if (czf.equals("update")) {//修改
            MedicineEntity medicineEntity = new MedicineEntity();
            //编号
            medicineEntity.setId(Integer.parseInt(req.getParameter("id")));
            //诊断号
            medicineEntity.setUserid(Integer.parseInt(req.getParameter("userid")));
            //患者名字
            medicineEntity.setUsername(req.getParameter("name"));
            //药物id
            medicineEntity.setMedicineid(Integer.parseInt(req.getParameter("drugname")));
            //药物数量
            medicineEntity.setSum(Integer.parseInt(req.getParameter("drugsum")));
            //事件
            medicineEntity.setTime(req.getParameter("time"));
            //医生id
            medicineEntity.setDid(Integer.parseInt(req.getParameter("doctorname")));
            //医嘱
            medicineEntity.setType(req.getParameter("type"));
            service.update(medicineEntity);//给update方法传入medicineEntity对象
            PrintWriter pw = resp.getWriter();//输入流
            String boo = JSON.toJSONString("true");//转JSON格式
            pw.print(boo);//要输出的东西
            pw.flush();//刷新
            pw.close();//关闭
        }
        Integer currPageNo_int = Integer.parseInt(currPageNo.trim());//给用户编号转为int类型
        int docid = Integer.parseInt(req.getParameter("docid"));//
        Page<MedicineEntity> page = service.MedicinePaging(docid, userName.trim(), currPageNo_int);//给MedicinePaging传入docid，userName，currPageNo_int
        req.setAttribute("docid", docid);//保存docid
        req.setAttribute("doctorname", req.getParameter("doctorname"));//保存doctorname
        req.setAttribute("MedicinePage", page);//保存page
        req.setAttribute("userName", userName);//保存userName
        req.getRequestDispatcher("doctor/MedicineSelect.jsp").forward(req, resp);//转发MedicineSelect.jsp
    }
}
